/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lucava.boatskeeper.client.view;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import org.lucava.boatskeeper.client.event.category.CancelEditCategoryEvent;
import org.lucava.boatskeeper.client.event.category.SaveCategoryEvent;
import org.lucava.boatskeeper.shared.dto.CategoryDto;
import org.lucava.lib.client.view.MvcView;

/**
 *
 * @author glenn
 */
public class CategoryEditView extends MvcView {

    private final TextBox displayName;
    private final Label displayNameMessage;
    private final FlexTable detailsTable;
    private final Button saveButton;
    private final Button cancelButton;
    private CategoryDto category;

    public CategoryEditView(EventBus eventBus) {
        super(eventBus);

        displayName = new TextBox();
        displayNameMessage = new Label();

        DecoratorPanel contentDetailsDecorator = new DecoratorPanel();
        contentDetailsDecorator.setWidth("40em");
        initWidget(contentDetailsDecorator);

        VerticalPanel contentDetailsPanel = new VerticalPanel();
        contentDetailsPanel.setWidth("100%");

        // Create the categorys list
        //
        detailsTable = new FlexTable();
        detailsTable.setCellSpacing(0);
        detailsTable.setWidth("100%");
        detailsTable.addStyleName("category-edit-table");


        initDetailsTable();
        contentDetailsPanel.add(detailsTable);

        HorizontalPanel menuPanel = new HorizontalPanel();
        saveButton = new Button("Save", new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                getEventBus().fireEvent(new SaveCategoryEvent());
            }
        });
        cancelButton = new Button("Cancel", new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                getEventBus().fireEvent(new CancelEditCategoryEvent());
            }
        });
        menuPanel.add(saveButton);
        menuPanel.add(cancelButton);
        contentDetailsPanel.add(menuPanel);
        contentDetailsDecorator.add(contentDetailsPanel);

    }

    private void initDetailsTable() {
        detailsTable.setWidget(0, 0, new Label("Nom"));
        detailsTable.setWidget(0, 1, displayName);
        detailsTable.setWidget(0, 2, displayNameMessage);
        displayName.setFocus(true);
    }

    public void setCategory(CategoryDto result) {
        this.category = result;
        displayName.setText(result.getName());
    }

    private void resetMessages() {
        displayNameMessage.setStyleName("red-text");
        displayNameMessage.setText("");
        displayNameMessage.setVisible(false);

    }

    public Boolean isValid() {

        Boolean isValid = true;

        if (!displayName.getText().matches(".{4,254}")) {
            displayNameMessage.setVisible(true);
            displayNameMessage.setText("minimum 4 caractères");
            isValid = false;
        }

        return isValid;

    }

    public CategoryDto getCategory() {
        category.setName(displayName.getText());
        return category;
    }
}
